﻿using Microsoft.AspNet.SignalR.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplicationTest
{
    public class User
    {
        public string Name { get; set; }
        public string Img { get; set; }

        public User()
        {

        }

        public User(string name, string img)
        {
            Name = name;
            Img = img;
        }

        public User(string name)
        {
            Name = name;
            Img = string.Empty;
        }
    }
    public class Message
    {
        public string Text { get; set; }
        public DateTime Date { get; set; }
        public User User { get; set; }

        public Message() { }

        public Message(string text, DateTime date, User user)
        {
            Text = text;
            Date = date;
            User = user;
        }

        public Message(string text, User user)
        {
            Text = text;
            Date = DateTime.Now.ToLocalTime();
            User = user;
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        HubConnection connection;
        IHubProxy myHub;

        public ObservableCollection<string> Messages { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Messages = new ObservableCollection<string>();

            connection = new HubConnection("http://localhost:55968/");

            myHub = connection.CreateHubProxy("ChatHub");
            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Connected");
                }

            }).Wait();
            myHub.Invoke<string>("Register", "New User WPF").ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error calling send: {0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine(task.Result);
                }
            });
            //myHub.On<string, string>("newMessage", (user, msg) =>
            //{
            //    GetMessages(user, msg);
            //}); 
            myHub.On<Message>("newMessage", (msg) =>
            {
                //GetMessages(user, msg);
                Console.WriteLine();
            });

            this.DataContext = this;

        }


        public void GetMessages(string user, string message)
        {
            string msg = string.Format("{0}: {1}", user, message);
            if (this.Dispatcher.CheckAccess())
            {
                Messages.Add(msg);
            }
            else
            {
                this.Dispatcher.Invoke(() => { Messages.Add(msg); }, DispatcherPriority.Normal);
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            connection.Stop();
            base.OnClosing(e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myHub.Invoke<string[]>("SendMessage", new[] { "New User WPF", Message.Text }).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error calling send: {0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine(task.Result);
                }
            });
            Message.Text = "";
        }
    }
}
